package op.app.my.demo.ceshi;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import op.core.common.mvc.api.R;
import op.core.common.utils.file.ExcelUtil;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.io.File;
import java.io.IOException;
/**
 * 测试表(Ceshi)表控制层
 *
 * @author xuan
 * @since 2023-01-30 15:45:40
 */
@RestController
@RequestMapping("ceshi")
public class CeshiApi {

    @Autowired
    private CeshiService ceshiService;
    @Autowired
    private CeshiRepo ceshiRepo;


    @GetMapping
    public R get(Ceshi ceshi, Page<Ceshi> page) {
        QueryWrapper<Ceshi> queryWrapper = new QueryWrapper<>();
        return R.ok(ceshiRepo.selectPage(page, queryWrapper));
    }


    @GetMapping("one/{id}")
    public R getOne(@PathVariable String id) {
        Ceshi main = ceshiRepo.selectById(id);
        return R.ok(main);
    }

    @PostMapping
    public R post(@RequestBody Ceshi main) {
        return R.ok(ceshiRepo.insert(main));
    }

    @PutMapping
    public R put(@RequestBody Ceshi main) {
        return R.ok(ceshiRepo.updateById(main));
    }

    @DeleteMapping("{ids}")
    public R delete(@PathVariable String[] ids) {
        return R.ok(ceshiRepo.deleteBatchIds(Arrays.asList(ids)));
    }

    @GetMapping("exportExcel")
    public R exportExcel( HttpServletResponse response) {
        QueryWrapper<Ceshi> queryWrapper = new QueryWrapper<>();
        List<Ceshi> ceshis = ceshiRepo.selectList(queryWrapper);
        List<CeshiDto> orderCatalogVOList = BeanUtil.copyToList(ceshis, CeshiDto.class);
        ExcelUtil.exportExcel(ceshis,"ceshi",Ceshi.class,response);
        return R.ok();
    }

    public static void main(String[] args) {
            // 开始读文字
        PDDocument doc = null;
        try {
            doc = PDDocument.load(new File("/Users/xuan/Downloads/IATF-16949-GM-CSR-Jan-2023.pdf"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        PDPageTree allPages = doc.getDocumentCatalog().getPages();

        doc.getClass();
//使用PDFTextStripper 工具
        PDFTextStripper tStripper = null;
        try {
            tStripper = new PDFTextStripper();
        } catch (IOException e) {
            e.printStackTrace();
        }
//设置文本排序，有规则输出
        tStripper.setSortByPosition(true);
//获取所有文字信息
        try {
            String info = tStripper.getText(doc);
            System.out.println(info);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

